package com.syh.jn.scm.service.impl.supplier.base.downstream;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.supplier.entity.base.downstream.Downstream;
import com.syh.jn.scm.domain.supplier.entity.base.downstream.DownstreamAccount;
import com.syh.jn.scm.domain.supplier.event.DownstreamEvent;
import com.syh.jn.scm.domain.supplier.repository.base.downstream.DownstreamAccountRepository;
import com.syh.jn.scm.domain.supplier.repository.base.downstream.DownstreamRepository;
import com.syh.jn.scm.dto.supplier.base.downstream.*;
import com.syh.jn.scm.dto.supplier.base.upstream.supplier.UpstreamSto;
import com.syh.jn.scm.service.api.supplier.base.downstream.DownstreamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 下游客户
 */
@Service
@Transactional
public class DownstreamServiceImpl extends AbstractServiceImpl<DownstreamSto, DownstreamMto, DownstreamVo, Downstream, Long> implements DownstreamService {

  @Autowired
  private DownstreamRepository downstreamRepositoryImpl;
  @Autowired
  private DownstreamAccountRepository downAccountRepositoryimpl;

  @Override
  protected AggregateRepository<Downstream, Long> getAggregateRepository() {
    return downstreamRepositoryImpl;
  }

  /**
   * 保存下游客户以及地址项
   *
   * @param dto
   * @return
   */
  @Override
  public Long save(DownstreamSto dto) {
    Downstream customer = Downstream.create(dto);
    Long id = downstreamRepositoryImpl.add(customer);

    Downstream downStreamMq = downstreamRepositoryImpl.getById(id);
    Object[] obj = new Object[]{downStreamMq};
    //发送事件
    this.publicEvent(DownstreamEvent.buildDownstreamCreatedEvent(obj));

    return id;
  }

  /**
   * 更改下游客户以及地址项
   *
   * @param mto
   */
  @Override
  public void update(DownstreamMto mto) {
    Downstream customer = downstreamRepositoryImpl.getById(mto.getId());
    customer.modify(mto);
    downstreamRepositoryImpl.update(customer);
  }

  /**
   * 删除下游客户以及地址项
   *
   * @param id
   */
  @Override
  public void delete(Long id) {
    Downstream downstream = downstreamRepositoryImpl.getById(id);
    downstream.destroy();
    downstreamRepositoryImpl.remove(downstream);
    this.publicEvent(DownstreamEvent.buildDownstreamDeletedEvent(new Object[]{downstream}));
  }

  /**
   * 启用
   *
   * @param id
   */
  @Override
  public void enabled(Long id) {
    Downstream downstream = downstreamRepositoryImpl.getById(id);
    downstream.enabled();
    downstreamRepositoryImpl.update(downstream, false);
  }

  /**
   * 停用下游客户以及地址项
   *
   * @param id
   */
  @Override
  public void disabled(Long id) {
    Downstream downstream = downstreamRepositoryImpl.getById(id);
    downstream.disabled();
    downstreamRepositoryImpl.update(downstream, false);
  }

  /**
   * 添加下游账号
   * @param sto
   * @return
   */
  @Override
  public Boolean addAccount(DownstreamAccountSto sto) {
    DownstreamAccount account = new DownstreamAccount(sto);
    return downAccountRepositoryimpl.addAccount(account);
  }

  /**
   * 修改下游账号
   * @param mto
   * @return
   */
  @Override
  public Boolean setAccount(DownstreamAccountMto mto) {
    DownstreamAccount account = downAccountRepositoryimpl.getById(mto.getId());
    account.modify(mto);
    ExtBeanUtils.copyProperties(mto,account,false);
    downAccountRepositoryimpl.update(account);
    return true;
  }


  /**
   * 绑定价格模板
   *
   * @param id              下游客户id
   * @param priceTemplateId 报价模板id
   */
  @Override
  public void bindingPriceTemplate(Long id, Long priceTemplateId) {
    Downstream downstream = downstreamRepositoryImpl.getById(id);
    downstream.bindingPriceTemplate(priceTemplateId);
    downstreamRepositoryImpl.update(downstream, false);
  }

  /**
   * 解除客户价格模板
   *
   * @param id
   */
  @Override
  public void removePriceTemplate(Long id) {
    Downstream downstream = downstreamRepositoryImpl.getById(id);
    downstream.removePriceTemplate();
    downstreamRepositoryImpl.update(downstream, false);
  }

  /**
   * 根据客户id获取客户地址
   *
   * @param downstreamId
   * @return
   */
  @Override
  public List<DownstreamAddressVo> getAddress(Long downstreamId) {
    return downstreamRepositoryImpl.getDownstreamAddressView(downstreamId);
  }

  @Override
  public List<Long> batchSave(List<DownstreamSto> downstreamStos) {
    List<Long> ids = new ArrayList<>();
    downstreamStos.stream().forEach(sto -> {
      Long id = this.save(sto);
      ids.add(id);
    });
    return ids;
  }
}
